home *** CD-ROM | disk | FTP | other *** search
- Path: atglab.bls.com!Alun.Champion
- From: Alun.Champion@bridge.bst.bls.com (Alun Champion)
- Newsgroups: comp.lang.c
- Subject: Re: Question about system call
- Date: 18 Jan 1996 23:49:36 GMT
- Organization: Computer People Inc.
- Message-ID: <ALUN.CHAMPION.96Jan18184936@g7240065.bridge.bst.bls.com>
- References: <4dekv8$9ja@spider.hik.se>
- <ALUN.CHAMPION.96Jan18120927@g7240065.bridge.bst.bls.com>
- <822002688snz@genesis.demon.co.uk>
- NNTP-Posting-Host: bstfirewall.bst.bls.com
- In-reply-to: Lawrence Kirby's message of Thu, 18 Jan 96 22:04:48 GMT
-
- In article <822002688snz@genesis.demon.co.uk> Lawrence Kirby <fred@genesis.demon.co.uk> writes:
-
- :>: An example:
- :>
- :>: write(0, (the integer value), sizeof(int));
-
- :>1) Try:
- :>
- :> write(0, &int_value, sizeof(int));
-
- : That assumes (the integer value) is an lvalue, not a general expression (or
- : even a constant).
-
- True, but you can always make it one.
-
- :>2) Try:
- :>
- :> char buf[10];
- :> sprintf(buf, "%d", int_value);
- :>
- :> write(0, buf, strlen(buf)-1);
-
- : strlen returns the number of characters in the string before the '\0' so
- : you don't want the -1. sprintf returns this value anyway so there's no
- : need to call strlen.
-
- I must be tired or something ;')
-
- :>A couple of points, this is not portable, why not use the stdio library,
- :>this would then become
- :>
- :>1)
- :> printf("%*s", sizeof(int), (char*)&int_value);
-
- : No, %s outputs a string i.e. stops if it hits a '\0' which it may well
- : do within the internal representation of an integer. Also * reads an
- : int argument so you need (int)sizeof(int). A close standard equivalent is:
-
- : fwrite(&int_val, sizeof(int), 1, stdout);
-
- Jeez, I fell asleep. This was stupid of me, it's been a while since I've
- used 'C' aggressively (currently using C++), and I totally forgot about
- this library function.
-
- BTW, This assumes (the integer value) is an lvalue, not a general expression
- (or even a constant ;')
-
- Regards
-
- -A.
-
- --
- | A.Champion |
-